//#include "ObjectPool.h" 本项目不实用ObjectPool.h， ObjectPool只是用来帮助理解freelist操作
#include "Concurrent.h"

void Alloc1()
{
	for (int i = 1; i <= 5; i++)
	{
		void *ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (int i = 1; i < 7; i++)
	{
		void *ptr = ConcurrentAlloc(7);
	}
}

void TLSTest()
{
	std::thread t1(Alloc1);
	t1.join();

	std::thread t2(Alloc2);
	t2.join();

	cout << sizeof(PAGE_ID) << endl;
	cout << sizeof(void*) << endl;	// 跨平台功能验证
}

void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);

	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
}

void TestConcurrentAlloc2()
{
	for (size_t i = 0; i < 1024; ++i)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << p1 << endl;
	}

	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	cout << p2 << endl;
	cout << p3 << endl;
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (size_t i = 0; i < 7; i++)
	{
		ConcurrentFree(v[i], 6);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (size_t i = 0; i < 7; i++)
	{
		ConcurrentFree(v[i], 6);
	}
}



void TestMultiThread()
{
	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);
	t1.join();
	t2.join();
}

int main()
{
	//TLSTest();//测试TLS是否能为每个线程分配一个独立变量
	//TestConcurrentAlloc1();
	//TestConcurrentAlloc2();
	//TestConcurrentAlloc2(); 
	TestMultiThread();
	return 0;
}